package vn.com.atm;

import org.json.JSONException;
import org.json.JSONObject;

import vn.com.atm.database.UserFunctions;
import vn.com.atm.library.Alert;
import vn.com.atm.library.SimpleCrypto;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
	Button btnLogin;
	TextView btnLinkToRegister;
	EditText inputName;
	EditText inputPassword;
	TextView loginErrorMsg;

	// JSON Response node names
	private static String KEY_SUCCESS = "status";
	public static String KEY_USERNAME = "userName";
	public static String KEY_STATUS_LOGIN = "STATUS_LOGIN";
	public static final String USER_PROFILE = "setting";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// Importing all assets like buttons, text fields
		inputName = (EditText) findViewById(R.id.lg_Account);
		inputPassword = (EditText) findViewById(R.id.lg_pass);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLinkToRegister = (TextView) findViewById(R.id.link_to_register);
		loginErrorMsg = (TextView) findViewById(R.id.login_error);

		// Login button Click Event
		btnLogin.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				String userName = inputName.getText().toString();
				String password = inputPassword.getText().toString();
				UserFunctions userFunction = new UserFunctions();
				SimpleCrypto sp = new SimpleCrypto();
				try {
					password = sp.encrypt("key", password);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				JSONObject json = userFunction.loginUser(userName, password);

				// check for login response
				try {
					if (json.getString(KEY_SUCCESS) != null) {
						loginErrorMsg.setText("");
						String res = json.getString(KEY_SUCCESS);
						if (res.equalsIgnoreCase("SUCCESS") == true) {
							
							// Luu thong tin dang nhap
							SharedPreferences preference = getSharedPreferences(USER_PROFILE, Activity.MODE_PRIVATE);
							SharedPreferences.Editor edit = preference.edit();
							edit.putBoolean(KEY_STATUS_LOGIN, true);
							edit.putString(KEY_USERNAME, userName);
							edit.commit();
							
							Intent mainActivity = new Intent(
									getApplicationContext(), MainActivity.class);
							mainActivity
									.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							mainActivity.putExtra("Status", "Success");
							mainActivity.putExtra(KEY_USERNAME, userName);

							startActivity(mainActivity);
							finish();
						} else {
							Alert a = new Alert();
							a.AlertError(LoginActivity.this, "Lỗi", "Đăng nhập thất bại do sai thông tin tài khoản/mật khẩu");
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

		// Link to Register Screen
		btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(),
						RegisterActivity.class);
				startActivity(i);
				finish();
			}
		});
	}
}